package com.fm.cotroller;

import com.fm.common.Const;
import com.fm.common.ResultResponse;
import com.fm.pojo.Product;
import com.fm.pojo.User;
import com.fm.service.IFileService;
import com.fm.service.IOrderService;
import com.fm.service.IProductService;
import com.fm.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Maps;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IFileService fileService;
    @Autowired
    private IOrderService orderService;

    //产品保存
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ResultResponse addProduct(HttpSession session, Product product){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            return productService.saveOrUpdateProduct(product);
        }
        return ResultResponse.isErrorMessage("抱歉，您还未获得商户权限！");
    }

    /**
     * 文件上传FTP
     * @param session
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("upload")
    @ResponseBody
    public ResultResponse upload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = fileService.upload(file,path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);
            return ResultResponse.isSuccess(fileMap);
        }else{
            return ResultResponse.isErrorMessage("无权限操作");
        }
    }

    //产品删除
    @RequestMapping(value = "del",method = RequestMethod.GET)
    public ResultResponse<String> delProduct(HttpSession session,String productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            return productService.delProduct(productId);
        }
        return ResultResponse.isErrorMessage("抱歉，您还未获得商户权限！");
    }

    //主页产品分页列表
    @RequestMapping(value = "allList",method = RequestMethod.GET)
    @ResponseBody
    public ResultResponse allList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        return productService.getProductList(pageNum,pageSize);
    }

    @RequestMapping(value = "myList",method = RequestMethod.GET)
    @ResponseBody
    public ResultResponse myList(HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        return productService.getMyProductList(user.getId(),pageNum,pageSize);
    }

    @RequestMapping("detail")
    @ResponseBody
    public ResultResponse detail(HttpSession session,String produceId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return productService.getProductDetail(produceId);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }

    //刷新发布时间
    @RequestMapping(value = "refreshTime",method = RequestMethod.GET)
    public ResultResponse<String> refreshTime(HttpSession session,String productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            int days=orderService.checkOrderDays(user.getId());
            return productService.refreshTime(productId,days);
        }
        return ResultResponse.isErrorMessage("抱歉，您还未获得商户权限！");
    }

    //修改发布状态
    @RequestMapping(value = "updateStatus",method = RequestMethod.GET)
    public ResultResponse<String> updateStatus(HttpSession session,String productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            int days=orderService.checkOrderDays(user.getId());
            return productService.updateStatus(productId,days);
        }
        return ResultResponse.isErrorMessage("抱歉，您还未获得商户权限！");
    }


}
